package com.baijiaoxi.common.utils.jdk;

import com.baijiaoxi.common.model.base.CommonError;
import com.baijiaoxi.common.model.base.CoreException;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class MapUtil {
    public static Long getLongValueFromMapNullToException(Map<String, String> map, String key) {
        Long longValue;
        String str = map.get(key);
        try {
            longValue = Long.valueOf(str);
        } catch (Exception e) {
            throw CoreException.create(CommonError.objectIsNull, e, key + "不存在或为空");
        }
        return longValue;
    }

    public static Long getLongValueFromMapNullToZero(Map<String, String> map, String key) {
        Long longValue = 0L;
        String str = map.get(key);
        try {
            longValue = Long.valueOf(str);
        } catch (Exception e) {
            log.error(key + "不存在或为空" + ">>>" + str);
        }
        return longValue;
    }


    public static Integer getIntegerValueFromMapNullToException(Map<String, String> map, String key) {
        Integer integerValue;
        String str = map.get(key);
        try {
            integerValue = Integer.valueOf(str);
        } catch (Exception e) {
            throw CoreException.create(CommonError.objectIsNull, e, key + "不存在或为空", str);
        }
        return integerValue;
    }

    public static Integer getIntegerValueFromMapNullToZero(Map<String, String> map, String key) {
        Integer integerValue = 0;
        String str = map.get(key);
        try {
            integerValue = Integer.valueOf(str);
        } catch (Exception e) {
            log.error(key + "不存在或为空" + ">>>" + str);
        }
        return integerValue;
    }


}
